package pers.mine.vo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import pers.mine.po.GoodsItem;

/**
 * @author Mine
 * @since 2016年10月24日_上午12:45:14
 */
public class Cart {

	private Map<Integer, GoodsItem> gsitMap = null;
	private double priceSum;
	private int numSum;

	public Cart() {
		this.gsitMap = new HashMap<Integer, GoodsItem>();
	}

	public double getPriceSum() {
		return priceSum;
	}

	public int getNumSum() {
		return numSum;
	}

	public int getGoodsItemSum() {
		return gsitMap.keySet().size();
	}

	@Override
	public String toString() {
		return "CartNumSum= [" + this.numSum + "]";
	}

	private void setSum() {
		if (gsitMap == null) {
			this.numSum = 0;
			this.priceSum = 0;
			return;
		}
		Collection<GoodsItem> ms = gsitMap.values();
		Iterator<GoodsItem> it = ms.iterator();
		double priceSum = 0;
		int numSum = 0;
		while (it.hasNext()) {
			GoodsItem gsit = it.next();
			numSum += gsit.getNum();
			priceSum += (gsit.getPrice() * gsit.getNum());
		}
		this.priceSum = priceSum;
		this.numSum = numSum;
	}

	/**
	 * 向购物车加入商品
	 * 
	 * @param dbgi
	 *            数据库新取出的信息
	 * @param plusNum
	 *            要添加的商品数量
	 * @return
	 */
	public boolean put(GoodsItem dbgi, Integer plusNum) {
		// 数据合法性检测
		if (!baseVali(dbgi, plusNum)) {
			return false;
		}
		Integer id = dbgi.getId();
		if (gsitMap.get(id) == null) {// 新添加的商品
			dbgi.setNum(plusNum);
			gsitMap.put(id, dbgi);
			this.setSum();// 设置新总数
			return true;
		} else {
			Integer newNum = plusNum + gsitMap.get(id).getNum();// 新旧数量之和
			return this.doUpdate(dbgi, newNum);
		}
	}

	/**
	 * 更新购物车商品数量
	 * 
	 * @param dbgi
	 * @param plusNum
	 * @return
	 */
	public boolean doUpdate(GoodsItem dbgi, Integer newNum) {
		// 数据合法性检测
		if (!baseVali(dbgi, newNum)) {
			return false;
		}
		Integer id = dbgi.getId();
		if (gsitMap.get(id) == null) {
			System.err.println("购物车不存在[id=" + id + "]的商品！");
			return false;
		} else {
			gsitMap.get(id).setNum(newNum); // 设置新的商品数量
		}
		this.setSum();// 设置新总数
		return true;
	}

	public boolean remove(Integer id) {
		if (gsitMap == null || gsitMap.keySet().size() < 1) {
			System.err.println("购物车为空！");
			return false;
		}
		if (gsitMap.get(id) == null) {
			System.err.println("购物车不存在[id=" + id + "]的商品！");
			return false;
		}
		gsitMap.remove(id);// 移除
		this.setSum();// 设置新总数
		return true;
	}

	public void clear() {
		this.priceSum = 0;
		this.numSum = 0;
		gsitMap.clear();
	}

	private boolean baseVali(GoodsItem onegi, Integer oneNum) {
		// 数据合法性检测
		if (onegi == null || onegi.getId() == null || onegi.getId() < 1) {
			return false;
		}
		if (oneNum == null || oneNum < 1 || oneNum > onegi.getNum()) {
			return false;
		}
		return true;
	}

	/**
	 * 获取一个商品List
	 * 
	 * @return
	 */
	public List<GoodsItem> getGoodsItemList() {
		return new ArrayList<GoodsItem>(gsitMap.values());
	}

}
